<?php

/**
 * Utility RSA 加密、解密
 */
class Rsa
{
    private $publicKey;
    private $privateKey;
    private $path;

    /**
     * 构造函数
     * @param string 公钥与私钥到目录
     */
    public function __construct($path)
    {
        if(empty($path) || !is_dir($path))
        {
            print("[error] error key path: " . $path ."\r\n");
        }
        $this->path = $path;
    }

    /**
     * 加载公钥
     * @return bool
     */
    public function setupPublicKey()
    {
        if(is_resource($this->publicKey))
        {
            return ture;
        }
        $file = $this->path . DIRECTORY_SEPARATOR . 'rsa_public_key.pem';
        $puk = file_get_contents($file);
        $this->publicKey = openssl_pkey_get_public($puk);
        return true;
    }

    /**
     * 加载私钥
     * @return bool
     */
    public function setupPrivateKey()
    {
        if(is_resource($this->privateKey))
        {
            return true;
        }
        $file = $this->path . DIRECTORY_SEPARATOR . 'private_key.pem';
        $prk = file_get_contents($file);
        $this->privateKey = openssl_pkey_get_private($prk);
        return true;
    }

    /**
     * 公钥加密
     * @param string 明文
     * @return string 密文
     */
    public function pubEncrypt($data)
    {
        if(!is_string($data))
        {
            return null;
        }
        $this->setupPublicKey();
        $ret = openssl_public_encrypt($data, $encrypted, $this->publicKey);
        if($ret)
        {
            return $encrypted;
        }
        else
        {
            return null;
        }
    }

    /**
     * 公钥解密
     * @param string 私钥加密得到的密文
     * @return string 明文
     */
    public function pubDecrypt($encrypted)
    {
        if(!is_string($encrypted))
        {
            return null;
        }
        $this->setupPublicKey();
        $ret = openssl_public_decrypt($encrypted, $data, $this->publicKey);
        if($ret)
        {
            return $data;
        }
        else
        {
            return null;
        }
    }

    /**
     * 私钥加密
     * @param string 明文
     * @return string 私钥加密得到的密文
     */
    public function priEncrypt($data)
    {
        if(!is_string($data))
        {
            return null;
        }
        $this->setupPrivateKey();
        $ret = openssl_private_encrypt( $data,$encrypted, $this->privateKey);
        if($ret)
        {
            return $encrypted;
        }
        else
        {
            return null;
        }
    }

    /**
     * 私钥解密
     * @param string 公钥加密得到的密文
     * @return string 明文
     */
    public function priDecrypt($encrypted)
    {
        if(!is_string($encrypted))
        {
            return null;
        }
        $this->setupPrivateKey();
        $ret = openssl_private_decrypt($encrypted, $data, $this->privateKey);
        if($ret)
        {
            return $data;
        }
        else
        {
            return null;
        }
    }

    /**
     * 关闭文件
     */
    public function __destruct()
    {
        @ fclose($this->publicKey);
        @ fclose($this->privateKey);
    }
}